﻿using System;
using System.Windows.Forms;
using DevExpress.LookAndFeel;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Helpers;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.ColorWheel;
using DevExpress.XtraNavBar;
using WinForm.Common;
using WinForm.Details;
using WinForm.Forms;
using WinForm.Modules;

namespace WinForm
{
    /// <summary>
    /// 继承于DevExpress.XtraBars.Ribbon.RibbonForm,AllowFormGlass:Flase，startPosition:CenterScreen
    /// 添加一个RibbonControl：rcMain，设置属性Ribbonstyle为office2010,设置帮助和视图菜单 
    /// 添加ribbonStatusBar：rbsmain,在其左侧的红色方框内添加两个staticText控件，分别设置其属性
    /// 添加PanelControl:pnlNavBarContainer,Dock:left,用于放置导航控件
    /// 在pnlMainContainer中添加NavBarControl:nbMain,Dock：Left
    /// 添加PanelControl:pnlUserControlContainer,Dock:Fill,用于放置表格等用户控件
    /// </summary>
    public partial class MainForm : RibbonForm
    {
        //the RibbonMenuController in this form
        //private  RibbonMenuController _ribbonMenuController = new RibbonMenuController(this);

        #region 字段

        #endregion

        #region 界面中的控件属性

        /// <summary>
        /// 左侧面板,用于放置导航控件nbMain
        /// </summary>
        public PanelControl NavBarContainer
        {
            get { return pnlNavBarContainer; }
        }

        /// <summary>
        /// 右侧面板，用于放置模块控件
        /// </summary>
        public PanelControl ModuleControlContainer
        {
            get { return pnlModuleControlContainer; }
        }

        // todo:完成状态栏的设置
        ///// <summary>
        ///// RibbonControl的 RibbonBarManager
        ///// </summary>
        //public IDXMenuManager MenuManager
        //{
        //    get { return rcMain.Manager; }
        //}

        /// <summary>
        /// Ribbon菜单
        /// </summary>
        public RibbonControl RibbonMenu
        {
            get { return rcMain; }
        }

        /// <summary>
        /// 左侧面板中放置的导航控件
        /// </summary>
        public NavBarControl NavigationBar
        {
            get { return nbMain; }
        }

        /// <summary>
        /// 最下面的状态栏
        /// </summary>
        protected internal RibbonStatusBar MainStatusBar
        {
            // todo:完成状态栏的设置2
            get { return rsbMain; }
        }

        protected internal BarButtonItem AddButton { get { return bbiAdd ; } }
        protected internal BarButtonItem EditButton { get { return bbiEdit; } }
        protected internal BarButtonItem DeleteButton { get { return bbiDelete; } }
        protected internal BarButtonItem RefreshButton { get { return bbiRefresh; } }
        protected internal BarButtonItem FirstButton { get { return bbiFirst; } }
        protected internal BarButtonItem PrevButton { get { return bbiPrevious ; } }
        protected internal BarButtonItem NextButton { get { return bbiNext; } }
        protected internal BarButtonItem LastButton { get { return bbiLast; } }
        internal BarButtonItem SaveButton { get { return bbiSave; } }
        internal BarButtonItem SaveAndCloseButton { get { return bbiSaveAndClose; } }
        internal BarButtonItem CloseButton { get { return bbiClose; } }
        protected FunctionModules MyFunctionModules { get { return FunctionModules.Instance; } }
        protected RibbonMenuController MyRibbonMenuController { get; private set; }


        protected internal BarButtonItem LargeIconButton { get { return bbiLargeIcon; } }
        protected internal BarButtonItem SmallIconButton { get { return bbiSmallIcon; } }
        protected internal BarButtonItem ListButton { get { return bbiList; } }
        protected internal BarButtonItem DetailsButton { get { return bbiDetails; } }
        protected internal BarButtonItem TileButton { get { return bbiTile; } }


        /// <summary>
        /// 当前模块
        /// </summary>
        public FunctionModule CurrentModule {
            get { return MyFunctionModules.CurrentModule; }
        }

        /// <summary>
        /// 当前模块
        /// </summary>
        public ModuleBase CurrentModuleControl {
            get { return CurrentModule!=null ? CurrentModule.UserControl : null;}
        }

        /// <summary>
        /// 当前 Detail 控件
        /// </summary>
        public DetailBase ActiveDetailControl {
            get { return CurrentModuleControl != null ? CurrentModuleControl.ActiveDetailControl : null; }
        }

        #endregion

        #region 构造及初始化
        public MainForm()
        {
            InitializeComponent();
            MyRibbonMenuController= new RibbonMenuController(this);
            //注册更新模块事件
            MyFunctionModules.CurrentModuleChanged += UpdateModuleView;
            //注册更新菜单事件
            MyFunctionModules.CurrentModuleChanged += UpdateMainMenu;
        }
        
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            //添加系统的功能模块
            RegisterModules();
            //把功能填充到导航栏
            MyFunctionModules.FillToNavBar(NavigationBar);
            //显示  关于 模块
            MyFunctionModules.SetCurrentModule("About");
            //初始化RibbonPage的显示
            MyRibbonMenuController.InitRibbonMenu();
            //为通用按钮注册按钮事件
            MyRibbonMenuController.InitButtonActions();
            //初始化皮肤控件
            SkinHelper.InitSkinGallery(rgbiSkins, true);
        }
        #endregion

        

        #region 菜单按钮事件
       
        protected virtual void bbColorMix_ItemClick(object sender, ItemClickEventArgs e)
        {
            using (var form = new ColorWheelForm())
            {
                form.StartPosition = FormStartPosition.CenterParent;
                form.SkinMaskColor = UserLookAndFeel.Default.SkinMaskColor;
                form.SkinMaskColor2 = UserLookAndFeel.Default.SkinMaskColor2;
                form.ShowDialog(this);
            }
        }


        protected void bbiAbout_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormAbout.ShowAbout(this);
        }

        /// <summary>
        /// 添加  按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bbiAdd_ItemClick(object sender, ItemClickEventArgs e)
        {
            CurrentModuleControl.Add();//调用Add后就有了ActiveDetailControl

            if (ActiveDetailControl == null)
            {
                throw new Exception(string.Format(ResourcesHelper.GetString("DetailControlHaveNotCreated"),
                    CurrentModule.Name + "'s Add"));
            }

            //注册事件
            CurrentModuleControl.ActiveDetailChanged += UpdateDetailView;
            CurrentModuleControl.ActiveDetailChanged += UpdateDetailMenu;

            //触发事件
            CurrentModuleControl.RaiseDetailChanged();
        }

        private void bbiEdit_ItemClick(object sender, ItemClickEventArgs e)
        {
            CurrentModuleControl.Edit();

            if (ActiveDetailControl == null)
            {
                throw new Exception(string.Format(ResourcesHelper.GetString("DetailControlHaveNotCreated"),
                    CurrentModule.Name + "'s Add"));
            }

            //注册事件
            CurrentModuleControl.ActiveDetailChanged += UpdateDetailView;
            CurrentModuleControl.ActiveDetailChanged += UpdateDetailMenu;

            //触发事件
            CurrentModuleControl.RaiseDetailChanged();
        }

        #endregion


        

        public void SetUserInfo(string caption) { bsUser.Caption = caption; }
        public void SetDataInfo(string caption) { bsData.Caption = caption; }

        /// <summary>
        /// 单击导航控件中的项  事件处理程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void nbMain_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            if (e.Link == null) return;
            //ShowModule(e.Link.Caption);
            MyFunctionModules.SetCurrentModule(e.Link.Caption);
        }

        #region 模块添加及显示

        /// <summary>
        /// 添加系统中的功能模块
        /// </summary>
        protected virtual void RegisterModules()
        {
            Init.FunctionModuleRegister();
        }

        /// <summary>
        /// 更新模块，当前模块发生变化时调用该方法
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="e"></param>
        protected virtual void UpdateModuleView(object obj, EventArgs e)
        {
            if (CurrentModuleControl==null) return;
            Cursor currentCursor = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                //获得新的要显示的模块的用户控件,并设置Dock方式
                if (CurrentModuleControl == null)
                {
                    throw new Exception(string.Format(ResourcesHelper.GetString("ModuleControlHaveNotCreated"),
                        CurrentModule.Name));
                }

                SuspendLayout();
                ModuleControlContainer.SuspendLayout();
                ModuleControlContainer.Controls.Clear();
                ModuleControlContainer.Controls.Add(CurrentModuleControl);
                CurrentModuleControl.Dock = DockStyle.Fill;
            }
            finally
            {
                Cursor.Current = currentCursor;
                ModuleControlContainer.ResumeLayout();
                ResumeLayout();
            }
        }

        /// <summary>
        /// 更新主菜单，当前模块发生变化时调用该方法
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="e"></param>
        protected virtual void UpdateMainMenu(object obj, EventArgs e)
        {
            MyRibbonMenuController.ShowRibbonPageForCurrentModule();
        }

        /// <summary>
        /// 更新Detail模块，当前Detail模块发生变化时调用该方法
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="e"></param>
        protected virtual void UpdateDetailView(object obj, EventArgs e)
        {
            if (ActiveDetailControl == null) return;
            Cursor currentCursor = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                //获得新的要显示的模块的用户控件,并设置Dock方式
                SuspendLayout();
                ModuleControlContainer.SuspendLayout();
                //ModuleControlContainer.Controls.Clear();
                ModuleControlContainer.Controls.Add(ActiveDetailControl);
                ActiveDetailControl.Dock = DockStyle.Fill;
                ActiveDetailControl.BringToFront();
            }
            finally
            {
                Cursor.Current = currentCursor;
                ModuleControlContainer.ResumeLayout();
                ResumeLayout();
            }
        }

        /// <summary>
        /// 更新Detail菜单，当前Detail模块发生变化时调用该方法
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="e"></param>
        protected virtual void UpdateDetailMenu(object obj, EventArgs e)
        {
            MyRibbonMenuController.ShowRibbonPageForActiveDetail();
        }
        #endregion

       


    }
}
